% This runs (2nd stage) regressions with true TFP innovations.
tfpshocks= [ tfpeps(2*lags+1:end),  tfpeps(2*lags:end-1),  tfpeps(2*lags-1:end-2),  tfpeps(2*lags-2:end-3) ];
%
% BIVARIATE:
ucoef2_z = zeros(3*lags+1, varseps2);
ycoef2_z = zeros(3*lags+1, varseps2);
for i = 1:varseps2
    % Unemployment regression: 
    Y = newY(:,1);
    X = [tfpshocks, dataxs(:,:,1), dataxs(:,:,i+1), ones(obs2,1)];
    if trend==1
        tx   = [ones(obs2,1), (2*lags:T)'];
        coef = (tx'*tx) \ (tx'*Y);
        Y    = Y - tx*coef;
    end
    ucoef2_z(:,i) = (X'*X)\(X'*Y);
    %
    % Regression for outcome i+1:
    Y = newY(:,i+1);
    if trend==1
        coef = (tx'*tx) \ (tx'*Y);
        Y    = Y - tx*coef;
    end
    ycoef2_z(:,i) = (X'*X)\(X'*Y);
end
%
%
% TRIVARIATE:
ucoef3_z = zeros(4*lags+1, varseps3);
fcoef3_z = zeros(4*lags+1, varseps3);
ycoef3_z = zeros(4*lags+1, varseps3);
for i = 1:varseps3
    % Unemployment regression: 
    Y = newY(:,1);
    X = [tfpshocks, dataxs(:,:,1), dataxs(:,:,2), dataxs(:,:,i+2), ones(obs2,1)];
    if trend==1
        tx   = [ones(obs2,1), (2*lags:T)'];
        coef = (tx'*tx) \ (tx'*Y);
        Y    = Y - tx*coef;
    end
    ucoef3_z(:,i) = (X'*X)\(X'*Y);
    %
    % Job finding regression: 
    Y = newY(:,2);
    if trend==1
        coef = (tx'*tx) \ (tx'*Y);
        Y    = Y - tx*coef;
    end
    fcoef3_z(:,i) = (X'*X)\(X'*Y);
    %
    % Regression for outcome i+2:
    Y = newY(:,i+2);
    if trend==1
        coef = (tx'*tx) \ (tx'*Y);
        Y    = Y - tx*coef;
    end
    ycoef3_z(:,i) = (X'*X)\(X'*Y);
end
